package com.service.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.admin.base.service.CoinsPriceService;
import com.service.admin.base.service.ConfigService;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.CoinsPriceMapper;
import com.service.dao.model.CoinsPrice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月27日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class CoinsPriceServiceImpl extends ServiceImpl<CoinsPriceMapper, CoinsPrice> implements CoinsPriceService {

    @Autowired
    private ConfigService configService;

    @Override
    public List<CoinsPrice> getCoinsPriceWithoutDefault() {
        return baseMapper.selectNewestPriceWithoutDefault(CommonStatusEnums.ACTIVE.getCode());
    }

    @Override
    public List<CoinsPrice> getCoinsPrice() {
        return baseMapper.selectNewestPrice(CommonStatusEnums.ACTIVE.getCode());
    }

    @Override
    public Map<String, BigDecimal> getCoinsPriceWithoutDefault2Map() {
        return this.getCoinsPriceWithoutDefault().stream()
                .collect(Collectors.toMap(CoinsPrice::getCoinsName, CoinsPrice::getCoinPrice));
    }

    @Override
    public Map<String, BigDecimal> getCoinsPrice2Map() {
        return this.getCoinsPrice().stream()
                .collect(Collectors.toMap(CoinsPrice::getCoinsName, CoinsPrice::getCoinPrice));
    }

    @Override
    public Map<String, BigDecimal> getStandardPriceWithoutDefault2Map() {
        return this.getCoinsPriceWithoutDefault().stream()
                .collect(Collectors.toMap(CoinsPrice::getCoinsName, CoinsPrice::getStandardPrice));
    }

    @Override
    public Map<String, BigDecimal> getStandardPrice2Map() {
        return this.getCoinsPrice().stream()
                .collect(Collectors.toMap(CoinsPrice::getCoinsName, CoinsPrice::getStandardPrice));
    }

    @Override
    public CoinsPrice getLastCoinPriceByCoinId(Integer coinId) {
        return getOne(new QueryWrapper<CoinsPrice>().lambda()
                .eq(CoinsPrice::getCoinsId, coinId)
                .orderByDesc(CoinsPrice::getId).last(" limit 1"));
    }

    @Override
    public CoinsPrice getLastCoinPriceByCoinName(String coinName) {

        CoinsPrice coinsPrice = getOne(new QueryWrapper<CoinsPrice>().lambda()
                .eq(CoinsPrice::getCoinsName, coinName)
                .orderByDesc(CoinsPrice::getId).last(" limit 1"));

        if(ObjectUtils.isEmpty(coinsPrice)){
            coinsPrice = new CoinsPrice();



        }

        return coinsPrice;
    }
}
